require File.dirname(__FILE__)+"/../../test_helper"
require "starframe/sprite"
require "starframe/sprite/collection"

class TestStarFrameSpriteCollection < Test::Unit::TestCase
  class TestSprite < StarFrame::Sprite
    attr_reader :texture, :updated
    
    @texture = StarRuby::Texture.new(1, 1)
    
    def update
      @updated = true
    end
  end
  
  def test_require
    assert StarFrame::Sprite::Collection.include?(Enumerable)
    sprite = TestSprite.new(0, 0)
    assert_respond_to sprite, :collection
    assert_respond_to sprite, :collection=
    assert_respond_to sprite, :vanish
  end
  
  def setup
    @collection = StarFrame::Sprite::Collection.new
  end
  
  def test_initialize
    assert_kind_of Array, @collection
    assert_equal 0, @collection.size
  end
  
  def test_add
    sprite1 = TestSprite.new(0, 0)
    @collection << sprite1
    assert_equal 1, @collection.size
    assert_equal @collection, sprite1.collection
    
    @collection << sprite1
    assert_equal 1, @collection.size
  end
  
  def test_add_duplication
    collection1 = StarFrame::Sprite::Collection.new
    collection2 = StarFrame::Sprite::Collection.new
    sprite = TestSprite.new(0, 0)
    
    collection1 << sprite
    assert_equal 1, collection1.size
    assert_equal collection1, sprite.collection
    
    collection2 << sprite
    assert_equal 1, collection2.size
    assert_equal 0, collection1.size
    assert_equal collection2, sprite.collection
  end
  
  def test_call
    @collection << sprite1 = TestSprite.new(1, 2)
    @collection << sprite2 = TestSprite.new(1, 2)
    
    assert !sprite1.updated
    assert !sprite2.updated
    @collection.call
    assert  sprite1.updated
    assert  sprite2.updated
  end
  
  def test_render_to
    sprite = TestSprite.new(1, 2)
    @collection << sprite
    texture = StarRuby::Texture.new 1, 1
    
    @collection.render_to texture
    assert_equal :render_texture, texture.last_method
    assert_equal sprite.texture,  texture.last_method_args[0]
    assert_equal 1,               texture.last_method_args[1]
    assert_equal 2,               texture.last_method_args[2]
  end
  
  def test_push
    
  end
  
  
  def test_sprite_vanish
    sprite = TestSprite.new(0, 0)
    @collection << sprite
    assert_equal 1, @collection.size
    assert_equal sprite, sprite.vanish
    assert_nil sprite.collection
    assert_equal 0, @collection.size
  end
  def test_sprite_vanish_uncollection
    sprite = TestSprite.new(0, 0)
    assert_equal sprite, sprite.vanish
    assert_nil sprite.collection
  end
end
